package com.vishop.daos;

import java.util.Collection;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.vishop.models.Merk;


@Repository
public class MerkDaoImpl {

	@Autowired
	private SessionFactory sessionFactory;
	
	public Collection<Merk> getAll() {
		return sessionFactory.getCurrentSession().createQuery("from Merk").list();
	}
	
	public Merk getById(int id){
		return (Merk) sessionFactory.getCurrentSession().get(Merk.class, id);
	}
	
	public int save(Merk merk){
		return (Integer) sessionFactory.getCurrentSession().save(merk);
	}	
	
	public void update(Merk merk)
	{
		sessionFactory.getCurrentSession().merge(merk);
	}
	
	public void delete(int id)
	{
		Merk c = getById(id);
		sessionFactory.getCurrentSession().delete(c);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public Merk getMerkbyProduct(int id){
		Query query = sessionFactory.getCurrentSession().createQuery("from Product where merk.merkId =:merkId");
		query.setParameter("merkId", 1);
		Collection productlijst = query.list();
		return (Merk) productlijst;
	}
	
	
}
